declare var nad:any;
class Message {	
	//连接状态
	public static session:any;
	private static heartFlag:boolean = true;
	private static chonglianNum:number = 0;//重连次数
	private static isConnect:boolean = false;//服务器链接状态
	public static CONNECT_CLOSE:string = "connectClose";
	/**
	 * 连接客户端
	 */	
	public static connectServer(config,callback,returnFun:Function=null){
		//如果1秒内没有链接成功就输出		
		this.isConnect = false;	
		if(returnFun){
			var lsTimer = egret.setTimeout(()=>{
			egret.clearTimeout(lsTimer);
			if(!Message.isConnect){
					//StageData.GameRoot.clew("游戏服务器链接未成功");
					//returnFun();
				}
			},this,2000);
		}
		//StageData.GameRoot.dispatchEventWith("connectClose");
		StageData.GameRoot.clew("开始连接服务器："+StageData.serverAddress);		
		nad.sessionFactory(StageData.serverAddress, config, (session)=>{
			StageData.GameRoot.clew("连接服务器成功");
			this.isConnect = true;
			this.startHeart();
			Message.session = session;
			this.commandHolder(session);
			callback();
		});		
	}
	public static connectFun(){
		if(!Message.isConnect){
			StageData.GameRoot.clew("游戏服务器链接未成功");
			StageData.MainObj.dispatchEventWith(Message.CONNECT_CLOSE);
		}
	}
	/**
	 * 向服务器广播消息
	 * @param data	广播的消息
	 */
	private static heart:boolean;//心跳属性 
	public static sendMsgToServer(data){	
		if (Message.session)	{
			this.heart = false;	
			Message.session.send(nad.SEvent(data));
		}
	}
	//开始发送心跳
	public static startHeart(){
		if(this.timer.running) {
			//console.log("心跳正在运行无需开启");
			return;
		}
		this.timer.addEventListener(egret.TimerEvent.TIMER,this.onTimer,this);
		this.timer.start();
	}
	//停止发送心跳
	public static stopHeart(){
		if(!this.timer.running) {
			//console.log("心跳已经停止无需再操作");
			return;
		}
		this.timer.removeEventListener(egret.TimerEvent.TIMER,this.onTimer,this);
		this.timer.stop();
		this.timer.reset();
	}

	//开始回放
	private static playbackData:any;
	private static playbackInt:number = 0;
	public static playTime:number = 1000;//回放播放间隔时间
	public static playStop:boolean = false;
	public static setPlayTimer(){
		if(this.playTime>500){
			this.playTime -=500;
		}else{
			this.playTime = 1000;
		}
	}
	public static toPlayback(e){
		console.log("回放:"+JSON.stringify(e));
		this.playbackData = e.data.videoList;
		this.playbackInt = 0;

		this.toplaybackMessage();
	}
	public static toplaybackMessage(){		
		if(this.playStop==false&&this.playbackInt<this.playbackData.length){
			let time:number = this.playTime;
			let playmsg:string = this.playbackData[this.playbackInt].commandCode;
			console.log('toplaybackMessage', this.playbackData[this.playbackInt]);
			if(playmsg=="11001"||playmsg=="11002"||playmsg=="20001"||playmsg=="20002"||playmsg=="100004"){
				time = 10;
			}			
			let huifangTimer = egret.setTimeout(()=>{
				egret.clearTimeout(huifangTimer);
				StageData.MainObj.dispatchEventWith(Events.MESSAGE_RECEIVE,false,this.playbackData[this.playbackInt]);
				this.playbackInt ++;
				this.toplaybackMessage();
			},this,time);
		}else{
			//alert("播放完成");
		}
	}
	/**
	 * 监听服务器消息
	 */
	public static timer:egret.Timer = new egret.Timer(1000,0);
	private static timeout;
	private static onTimer(e:egret.TimerEvent){
		var lsthis = this;
		Message.session.send(nad.HEvent());
		lsthis.heartFlag = false;	
		this.timeout = setTimeout(function(){
			egret.clearTimeout(this.timeout);
			if (lsthis.heartFlag == false) {
					if(lsthis.chonglianNum < 3){//重连3次
						lsthis.chonglianNum ++;
					}else{
						//console.log("\n\n\n\n已断线！！");
						lsthis.chonglianNum = 0;
						StageData.MainObj.dispatchEventWith(Events.MESSAGE_BREAK);
					}
			}
		},500);
	}

	public static commandHolder(session){
		session.addHandler(nad.SESSION_MESSAGE,(evt)=>{
			let source = evt.source;
			this.heartFlag = true;//返回心跳
			this.chonglianNum = 0;
			nad.Offline = false;
			if(!source.commandCode){
				return;
			}
			StageData.MainObj.dispatchEventWith(Events.MESSAGE_RECEIVE,false,source);			
		})
	}
}